<template>
  <div>
    <!-- 面包屑导航组件 -->
    <MBX text="权限管理" text1="角色列表"></MBX>
    <!-- 卡片 -->

    <!-- 布局 -->
    <el-card>
      <!-- 分栏间隔 -->
      <el-row>
        <el-col>
          <!-- 添加角色按钮 -->
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table :data="rolesList" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- 循环第一级权限 -->
            <el-row
              style="border-left: 1px solid #eee;"
              :class="['veieing','bdbottom',index===0?'botop':'']"
              v-for="(item,index) in scope.row.children"
              :key="item.id"
            >
              <el-col :span="5">
                <el-tag closable @close="handleClose(scope.row,item.id)">{{item.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <!-- 循环第二级权限 -->
                <el-row
                  style="border-left: 1px solid #eee;border-right: 1px solid #eee;"
                  :class="['veieing',index2==0?'':'botop']"
                  v-for="(item2,index2) in item.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag
                      closable
                      @close="handleClose(scope.row,item2.id)"
                      type="success"
                    >{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <!-- 循环三级权限 -->
                    <el-tag
                      type="warning"
                      closable
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      @close="handleClose(scope.row,item3.id)"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-tooltip effect="dark" content="修改" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            </el-tooltip>
            <!-- 删除按钮 -->
            <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            </el-tooltip>
            <!-- 分配权限按钮 -->
            <el-tooltip effect="dark" content="分配权限" placement="top" :enterable="false">
              <el-button
                type="warning"
                icon="el-icon-setting"
                size="mini"
                @click="fenPei(scope.row)"
              >分配权限</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格 -->
    </el-card>
    <!-- 布局 -->
    <!-- 分配权限弹窗 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="50%" @close="setRightDialogClosed">
      <!-- 树形空间 -->
      <el-tree
        show-checkbox
        default-expand-all
        node-key="id"
        :data="allList"
        :props="defaultProps"
        :default-checked-keys="defKeys"
        ref="treeRef"
      ></el-tree>
      <span slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allotRights">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分配权限弹窗 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 角色列表数据
      rolesList: [],
      // 分配权限窗口
      dialogVisible: false,
      // 所有权限列表数据
      allList: [],
      // 树形控件
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      // 树形控件选中的id
      defKeys: [],
      // 角色id
      roleld: ''
    }
  },
  created() {
    // 获取角色列表数据
    this.getRolesList()
  },
  methods: {
    // 获取角色列表数据
    async getRolesList() {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        return this.$message.error('角色列表数据获取失败')
      }
      this.rolesList = res.data
    },
    // 点击xx删除当前
    async handleClose(scope, id) {
      // 调用弹框组件
      const res = await this.$prompt(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // 判断返回值
      if (res !== 'confirm') return this.$message.error('取消了删除')
      // 发送请求删除数据
      const { data: result } = await this.$http.delete(
        `roles/${scope.id}/rights/${id}`
      )
      // 判断返回的状态码
      if (result.meta.status !== 200) return this.$message.error('用户删除失败')
      // 状态值是200就返回成功信息
      this.$message.success('用户删除成功')
      // 当前的数据等于反正的角色最新数据
      scope.children = result.data
    },
    // 分配权限
    async fenPei(row) {
      // 点击分配权限的时候把当前角色id存起来
      this.roleld = row.id
      const { data: res } = await this.$http.get('rights/tree')
      // 判断返回的状态码
      if (res.meta.status !== 200) {
        return this.$message.error('获取用户权限数据失败')
      }
      this.allList = res.data
      // 把值传递然后递归
      this.getLeafKeys(row, this.defKeys)
      this.dialogVisible = true
    },
    // 过去用户权限的id 递归
    getLeafKeys(row, arr) {
      // 如果里面没有children就把数组传递进去
      if (!row.children) {
        return arr.push(row.id)
      }
      // 如果还有children就传给事件继续递归
      row.children.forEach(item => {
        this.getLeafKeys(item, arr)
      })
    },
    // 关闭弹窗清空数组
    setRightDialogClosed() {
      this.defKeys = []
    },
    // 点击确定关闭窗口发送请求
    async allotRights() {
      // 获取全部选中和半选中的id
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedNodes()
      ]
      // 再把每个id以逗号分隔
      const idStr = keys.join(',')
      const { data: res } = await this.$http.post(
        `roles/${this.roleld}/rights`,
        { rids: idStr }
      )
      if (res.meta.status !== 200) {
        return this.$message.error('权限更改失败')
      }
      this.$message.success('权限更改成功')
      this.getRolesList()
      // 隐藏对话框
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
</style>
